//排队问题
/*
输入
4
1 0
2 1
1 0
2
3
3

输出
2 4 1

*/
#include <stdio.h>
#include <stdlib.h>

#define MAX_N 100005

int prev[MAX_N];
int next[MAX_N];
int deleted[MAX_N];

int main()
{
    int n;
    scanf("%d", &n);

    prev[1] = 0;
    next[1] = 0;

    for (int i = 2; i <= n; i++)
    {
        int k, p;
        scanf("%d %d", &k, &p);
        if (p == 0)
        {
            int left_prev = prev[k];
            prev[i] = left_prev;
            next[i] = k;
            if (left_prev != 0)
            {
                next[left_prev] = i;
            }
            prev[k] = i;
        }
        else
        {
            int right_next = next[k];
            prev[i] = k;
            next[i] = right_next;
            if (right_next != 0)
            {
                prev[right_next] = i;
            }
            next[k] = i;
        }
    }

    int m;
    scanf("%d", &m);
    for (int i = 0; i < m; i++)
    {
        int x;
        scanf("%d", &x);
        if (!deleted[x])
        {
            deleted[x] = 1;
        }
    }

    int head = 1;
    while (prev[head] != 0)
    {
        head = prev[head];
    }

    for (int current = head; current != 0; current = next[current])
    {
        if (!deleted[current])
        {

            printf("%d ", current);
        }
    }
    printf("\n");

    return 0;
}



//图的连接问题
/*输入
5 6  (5代表5个顶点,6代表6条无向边)
1 3
2 4
1 4
2 3
3 5
2 5
输出
2 4 3
3 5 3 4
3 5 2 1
2 1 2
2 2 3
*/


#include <stdio.h>

#define MAX_N 5001
#define MAX_M 100001

// 边的数组
int edges[MAX_M * 2];
// 表头数组，存储每个顶点的第一条边的编号
int head[MAX_N];
// 下一条边的数组，存储同一起点的下一条边的编号
int next[MAX_M * 2];
// 记录每个顶点的边的数量
int edgeCount[MAX_N];
// 边的编号(索引)
int idx;

// 添加边的函数
void addEdge(int a, int b)
{
    edges[idx] = b;//malloc创建一个结点edges[idx],这个结点存放b
    next[idx] = head[a];//malloc创建的这个结点指向头结点的下一个
    head[a] = idx++;//头结点指向malloc创建的这一个结点
    edgeCount[a]++;//用于计数
}

int main()
{
    int n, m;
    scanf("%d %d", &n, &m);

    // 初始化表头数组
    for (int i = 1; i <= n; i++)
    {
        head[i] = -1;
    }

    // 读取边的信息
    for (int i = 0; i < m; i++)
    {
        int a, b;
        scanf("%d %d", &a, &b);
        // 无向边需要添加两条边
        addEdge(a, b);
        addEdge(b, a);
    }

    // 输出每个顶点的相邻顶点
    for (int i = 1; i <= n; i++)
    {
        printf("%d", edgeCount[i]);
        for (int j = head[i]; j != -1; j = next[j])
        {
            printf(" %d", edges[j]);
        }
        printf("\n");
    }

    return 0;
}

